package com.huike.pmps.service.impl;

import com.huike.pmps.dao.CockpitAdminDao;
import com.huike.pmps.dao.NewSignDao;
import com.huike.pmps.dao.PaymentCollectionDao;
import com.huike.pmps.model.dto.cockpit.admin.BusinessOpportunityLineAdminDTO;
import com.huike.pmps.model.dto.cockpit.admin.BusinessOpportunityLineFormatAdminDTO;
import com.huike.pmps.model.dto.cockpit.admin.NewSignAdminDTO;
import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.CockpitAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Service
public class CockpitAdminServiceImpl implements CockpitAdminService {
    @Autowired
    NewSignDao newSignDao;
    @Autowired
    PaymentCollectionDao paymentCollectionDao;
    @Autowired
    CockpitAdminDao cockpitAdminDao;

    /**
     * 管理员-新合同数量及金额
     */
    @Override
    public Result selectNewSignData(Member member, Integer year, Integer month) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        if (year != null && month != null && month == 0) {

            List<NewSignAdminDTO> newSignAdminDTOS = newSignDao.selectAdminNewSignData(year, null);
            this.selectAdminNewSignDataChild(newSignAdminDTOS, year, month);
            return Result.success(this.selectAdminNewSignDataChild(newSignAdminDTOS, year, null));

        } else if (year != null && month != null && month != 0) {

            List<NewSignAdminDTO> newSignAdminDTOS = newSignDao.selectAdminNewSignData(year, month);
            this.selectAdminNewSignDataChild(newSignAdminDTOS, year, month);
            return Result.success(this.selectAdminNewSignDataChild(newSignAdminDTOS, year, month));

        } else {

            List<NewSignAdminDTO> newSignAdminDTOS = newSignDao.selectAdminNewSignData(LocalDate.now().getYear(), LocalDate.now().getMonthValue());
            this.selectAdminNewSignDataChild(newSignAdminDTOS, year, month);
            return Result.success(this.selectAdminNewSignDataChild(newSignAdminDTOS, LocalDate.now().getYear(), LocalDate.now().getMonthValue()));

        }

    }

    /**
     * 管理员-产品销售金额数据
     */
    @Override
    public Result selectProductNewSignContractAmount(Member member, String regionName, String provinceName, Integer year, Integer month) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        if (year != null && month != null && month == 0) {
            return Result.success(newSignDao.selectAdminProductNewSignContractAmount(regionName, provinceName, year, null));
        } else if (year != null && month != null && month != 0) {
            return Result.success(newSignDao.selectAdminProductNewSignContractAmount(regionName, provinceName, year, month));
        } else {
            return Result.success(newSignDao.selectAdminProductNewSignContractAmount(regionName, provinceName, LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    /**
     * 管理员-周期回款情况
     */
    @Override
    public Result selectPaymentCollectAmount(Member member, Integer year) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        return Result.success(paymentCollectionDao.selectAdminPaymentCollectAmount(year));
    }

    @Override
    public Result selectGrossProfit(Member member, Integer year, Integer month) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        if (year != null && month != null && month == 0) {
            return Result.success(cockpitAdminDao.getGrossProfitBy(year, null));
        } else if (year != null && month != null && month != 0) {
            return Result.success(cockpitAdminDao.getGrossProfitBy(year, month));
        } else {
            return Result.success(cockpitAdminDao.getGrossProfitBy(LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    @Override
    public Result selectFinance(Member member, Integer year, Integer month) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        if (year != null && month != null && month == 0) {
            return Result.success(cockpitAdminDao.getFinance(year, null));
        } else if (year != null && month != null && month != 0) {
            return Result.success(cockpitAdminDao.getFinance(year, month));
        } else {
            return Result.success(cockpitAdminDao.getFinance(LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    @Override
    public Result selectOnLineCourseOperation(Member member, Integer year, Integer month) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        if (year != null && month != null && month == 0) {
            return Result.success(cockpitAdminDao.getOnLineCourseOperation(year, null));
        } else if (year != null && month != null && month != 0) {
            return Result.success(cockpitAdminDao.getOnLineCourseOperation(year, month));
        } else {
            return Result.success(cockpitAdminDao.getOnLineCourseOperation(LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    @Override
    public Result selectBusinessOpportunities(Member member, Integer year, Integer month) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        List<BusinessOpportunityLineAdminDTO> list = null;
        if (year != null && month != null && month == 0) {
            list = cockpitAdminDao.getBusinessOpportunities(year, null);
        } else if (year != null && month != null && month != 0) {
            list = cockpitAdminDao.getBusinessOpportunities(year, month);
        } else {
            list = cockpitAdminDao.getBusinessOpportunities(LocalDate.now().getYear(), LocalDate.now().getMonthValue());
        }

        List<BusinessOpportunityLineFormatAdminDTO> list2 = new ArrayList<>();

        List<String> regionList = new ArrayList<>();

        if (list.size() > 0) {
            for (BusinessOpportunityLineAdminDTO item : list) {
                if (!regionList.contains(item.getRegionName())) {
                    regionList.add(item.getRegionName());
                }
            }
            for (String regionName : regionList) {
                BusinessOpportunityLineFormatAdminDTO dto = new BusinessOpportunityLineFormatAdminDTO();
                List<BusinessOpportunityLineAdminDTO> child = new ArrayList<>();
                for (BusinessOpportunityLineAdminDTO item : list) {
                    if (item.getRegionName().equals(regionName)) {
                        child.add(item);
                    }
                }
                dto.setRegionName(regionName);
                dto.setChild(child);
                list2.add(dto);
            }
        }


        return Result.success(list2);
    }

    @Override
    public Result selectBusinessOpportunitiesStage30(Member member, Integer year, Integer month) {

        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        if (year != null && month != null && month == 0) {
            return Result.success(cockpitAdminDao.getBusinessOpportunitiesStage30(year, null));
        } else if (year != null && month != null && month != 0) {
            return Result.success(cockpitAdminDao.getBusinessOpportunitiesStage30(year, month));
        } else {
            return Result.success(cockpitAdminDao.getBusinessOpportunitiesStage30(LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    @Override
    public Result selectOnLineCourseStock(Member member) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        return Result.success(cockpitAdminDao.getOnLineCourseStock());
    }

    @Override
    public Result selectCourseCapacity(Member member) {
        //管理员
        if (!member.getRolesIds().contains(1)) return Result.failure("您的账号不是管理员角色，无权查看");
        return Result.success(cockpitAdminDao.getCourseCapacity());
    }

    /**
     * 获取战区下的省区新签数据
     *
     * @param newSignAdminDTOList
     * @return
     */
    private List<NewSignAdminDTO> selectAdminNewSignDataChild(List<NewSignAdminDTO> newSignAdminDTOList, Integer year, Integer month) {
        if (newSignAdminDTOList.size() > 0) {
            for (int i = 1; i < newSignAdminDTOList.size(); i++) {
                NewSignAdminDTO newSignAdminDTO = newSignAdminDTOList.get(i);
                List<NewSignAdminDTO> selectAdminNewSignDataChild = newSignDao.selectAdminNewSignDataChild(newSignAdminDTO.getRegionName(), year, month);
                if (selectAdminNewSignDataChild.size() > 0) {
                    newSignAdminDTO.setChildren(selectAdminNewSignDataChild);
                }
            }
        }
        return newSignAdminDTOList;
    }
}
